package com.socialbank.web.util;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

public class JsonUtil {

	public static String listMapToJsonString(List<Map<String, Object>> manyContents) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		//objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(manyContents);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
	
	/**
	 * 不处理Long类型的装换
	 * @param manyContents
	 * @return
	 */
	public static String listMapToJsonStringNotLong(List<Map<String, Object>> manyContents) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		//SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		//simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		//simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		//objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(manyContents);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}

	public static String mapToJsonString(Map<String, Object> manyContents) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.registerModule(simpleModule);
		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		//objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(manyContents);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
	public static String objectToJsonString(Object object) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.registerModule(simpleModule);
		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		//objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
	/**
	 * 去除空
	 * @param object
	 * @return
	 */
	public static Object objectRemoveNull(Object object) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.registerModule(simpleModule);
		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		//objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(object);
			return objectMapper.readValue(jsonArray, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new HashMap<>();
	}
	
}
